package com.hspedu.hspliving.commodity.controller;

import com.hspedu.common.utils.PageUtils;
import com.hspedu.hspliving.commodity.entity.SkuInfoEntity;
import com.hspedu.hspliving.commodity.service.SkuInfoService;
import com.hspedu.hspliving.commodity.vo.SearchResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 */
@Controller
public class SearchController {

    //装配SkuInfoService
    @Resource
    private SkuInfoService skuInfoService;

    /**
     * 1. 家居网前台（购买用户）-检索页面(3-104)
     * 2. 如果将来检索的时候有检索条件，还需要进行处理..
     * 3. 用户提交的检索条件，我们封装到 params
     *
     * @return
     */
    @RequestMapping("/list.html")
    public String searchList(@RequestParam Map<String, Object> params, Model model) {

        //分页查询
        SearchResult searchResult =
                skuInfoService.querySearchPageByCondition(params);


//        //将pageUtils(PageUtils) 相关的数据取出，根据业务需求进行二次封装成 SearchResult
//
//        SearchResult searchResult = new SearchResult();
//
//        searchResult.setCommodity((List<SkuInfoEntity>) page.getList());
//        searchResult.setPageNum(page.getCurrPage());
//        searchResult.setTotal((long) page.getTotalCount());
//        int totalPage = page.getTotalPage();
//        searchResult.setTotalPages(totalPage);
//
//
//        List<Integer> pageNavs = new ArrayList<>();
//        //遍历总的页数，得到 pageNavs
//        for (int i = 1; i <= totalPage; i++) {
//            pageNavs.add(i);
//        }
//        searchResult.setPageNavs(pageNavs);
//
//        searchResult.setKeyword
//                (params.get("keyword") == null ? "" : params.get("keyword").toString());
//
//
        //因为我们得到searchResult 是需要给html模板使用,
        // 将 searchResult 放入model
        model.addAttribute("result", searchResult);

        return "list";
    }
}
